/* ProductCategoryBusiness.java
 *
 * Project/Product: GW
 *
 * Modify Information:
 * =============================================================================
 *   Author	Date       Description
 *   ----------	---------- ---------------------------------------------------
 *   Administrator	2008-7-9	First release
 *
 * Licensed Materials - Property of APEP
 * =============================================================================
 *    (C) COPYRIGHT Shanghai Auto Mart E-Procurement Co., Ltd. 2007 - 2008
 *    All Rights Reserved.
 *
 *    This software is the confidential and proprietary information of
 *    Shanghai Auto Mart E-Procurement Co., Ltd. ("Confidential Information").
 *    You shall not disclose such Confidential Information and shall use it only
 *    in accordance with the terms of the license agreement you entered into 
 *    with Shanghai Auto Mart E-Procurement Co., Ltd.
 *
 * Noties:
 * =============================================================================
 * 
 */

package system.download;

import java.util.Map;
import java.util.Vector;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import framework.core.DBConnection;

public class ProductCategoryBusiness {
    private Business business = null;
    
    ProductCategoryBusiness(){
        business = Business.getBusiness();
    }
    
    String[] colNamesCate = new String[]{
            "CODE",
            "NAME",
            "UPPER_CATEGORY",
            "CATEGORY_LEVEL"}; 
    
    
    public HSSFWorkbook createCategory(String preDay,String sheetName,Vector vec) {
        //创建文件对象
          HSSFWorkbook tWb = new HSSFWorkbook();
          //创建sheet
          HSSFSheet tSheet = tWb.createSheet();
          //命名sheet
          tWb.setSheetName(0,sheetName,HSSFCell.ENCODING_UTF_16);
          //创建行
          HSSFRow tRow = tSheet.createRow((short)0);
          int size = vec.size();
          HSSFCell tCell0  = business.createCell(tWb,tRow,(short)0, "总行数:");
          HSSFCell tCell1  = business.createCell(tWb,tRow,(short)1, String.valueOf(size));
          for(int i=0;i<size;i++){
              Map map = (Map)vec.get(i);
              HSSFRow row = tSheet.createRow((short)i+1);
              HSSFCell cell0  = business.createCell(tWb,row,(short)0, map.get("CODE")== null ? "" : String.valueOf(map.get("CODE")));
              HSSFCell cell1  = business.createCell(tWb,row,(short)1, map.get("NAME")== null ? "" : String.valueOf(map.get("NAME")));
              HSSFCell cell2  = business.createCell(tWb,row,(short)2, map.get("UPPER_CATEGORY")== null ? "" : String.valueOf(map.get("UPPER_CATEGORY")));
              HSSFCell cell3  = business.createCell(tWb,row,(short)3, map.get("CATEGORY_LEVEL")==null ? "" : String.valueOf(map.get("CATEGORY_LEVEL")));
          } 
          return tWb;
      }
    
    public void searchCategory(String preDay, DBConnection con) {
        String sql = "select * from T_PRODUCT_CATEGORY where last_update_time >= '" + preDay + business.start + "' and last_update_time <= '" + preDay + business.end + "'";
        Vector vec = business.setMap(colNamesCate, sql, con);
        Map map = null;
        // 获得路径
        String path = FileBusiness.getRealPath();
        String filename = business.getPreDayNum()+"_000000_材料分类文件.xls";
        if (vec.size() > 0) {
            // 创建文件
            HSSFWorkbook book = createCategory(preDay, filename, vec);
            // 写文件
            boolean result = FileBusiness.createExcelFile(path, book, filename);
            // 写报告
            if (result){
                map = business.createMap("000000", "T_PRODUCT_CATEGORY", path, filename, "成功");
            }
            else{
                map = business.createMap("000000", "T_PRODUCT_CATEGORY", path, filename, "失败");
            }
        } else{
            map = business.createMap("000000", "T_PRODUCT_CATEGORY", path, filename, "无更新");
        }
        business.writeReport(con, map);
    }
}
